#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<string.h>
#include<math.h>

void reverse(int* left, int* right)
{
    while (left < right)
    {
        int temp = *left;
        *left = *right;
        *right = temp;
        left++;
        right--;
    }
}
int main()
{
    int n = 0, m = 0, x = 0, len = 0, i = 0, j = 0, k = 0, a[100] = { 0 };
    char arr[100] = { 0 }, temp[] = "abcdef", str[100] = { 0 };
    scanf("%d %s %d", &n, arr, &m);
    long long y = 0;
    len = strlen(arr);
    for (i = len - 1; i >= 0; i--)
    {
        if (arr[i] >= '0' && arr[i] <= '9')
            x = arr[i] - '0';
        else
        {
            for (j = 0; j < 6; j++)
            {
                if (arr[i] == temp[j])
                {
                    x = arr[i] - 87;
                    break;
                }
                else if (arr[i] == temp[j] - 32)
                {
                    x = arr[i] - 55;
                    break;
                }
            }
        }
        y += x * pow(n, k++);
    }
    i = 0;
    while (y)
    {
        a[i++] = y % m;
        y /= m;
    }
    reverse(a, a + i - 1);
    for (j = 0; j < i; j++)
    {
        if (a[j] >= 10 && a[j] <= 15)
            str[j] = a[j] + 55;
        else
            str[j] = a[j] + '0';
    }
    if (strcmp(arr, "0") == 0)
        printf("0\n");
    else
        puts(str);
    return 0;
}